/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package com.xiashitech.agent.instrumentation.tomcat.valve.standardengine.inst;

import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

import static net.bytebuddy.matcher.ElementMatchers.*;

public class XSStandardEngineInstrumentation implements TypeInstrumentation {
  private final String standardEngineValveClassName;

  public XSStandardEngineInstrumentation(String standardEngineValveClassName) {
    this.standardEngineValveClassName = standardEngineValveClassName;
  }

  @Override
  public ElementMatcher<TypeDescription> typeMatcher() {
    return named("org.apache.catalina.core.StandardEngineValve");
  }

  @Override
  public void transform(TypeTransformer transformer) {
    transformer.applyAdviceToMethod(
        isMethod()
            .and(isPublic()).and(isFinal())
            .and(named("invoke"))
            .and(takesArgument(0, named("org.apache.catalina.connector.Request")))
            .and(takesArgument(1, named("org.apache.catalina.connector.Response"))),
            standardEngineValveClassName);
  }
}
